package com.x.client.utils

import android.annotation.TargetApi
import android.databinding.adapters.TextViewBindingAdapter
import android.databinding.adapters.TextViewBindingAdapter.setText
import android.graphics.drawable.Drawable
import android.os.Build
import android.support.annotation.ColorInt
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.gg.widget.navigationbar.OnTextChangeListener
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.x.client.App
import com.x.client.R
import com.x.client.R.id.description
import com.x.client.R.id.subtext
import java.util.concurrent.TimeUnit

/**
 * Creator : GG
 * Date    : 2018/5/22
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
object UIHelper {


    fun setItem(view: View, title: String, isCheck: Boolean = false, @ColorInt selectTextColor: Int = ContextCompat.getColor(App.instance, R.color.text_orange)
                , @ColorInt unSelectTextColor: Int = ContextCompat.getColor(App.instance, R.color.text_black), @DrawableRes rightImg: Int, action: View.OnClickListener) {
        val textView = view.findViewById<TextView>(R.id.title)
        val stateImg = view.findViewById<ImageView>(R.id.stateImg)
        stateImg.setImageResource(rightImg)
        textView.text = title
        if (isCheck) {
            textView.setTextColor(selectTextColor)
            stateImg.visibility = View.VISIBLE
        } else {
            textView.setTextColor(unSelectTextColor)
            stateImg.visibility = View.INVISIBLE

        }

        view.setOnClickListener(action)
    }

    fun setAccessible(view: View, icon: Int = -1, text: String = "", subtext: String = "", description: String = "", subColor: Int = 0,
                      descriptionColor: Int = 0, showRightImg: Boolean = true, onClickListener: View.OnClickListener? = null) {
        val subtextView = view.findViewById<TextView>(R.id.subtext)
        if (subColor != 0)
            subtextView.setTextColor(subColor)
        subtextView.text = subtext

        if (icon != -1) {
            val imageView = view.findViewById<ImageView>(R.id.icon)
            imageView.setImageResource(icon)
            imageView.visibility = View.VISIBLE
        }

        view.findViewById<View>(R.id.rightImg).visibility = if (showRightImg) View.VISIBLE else View.INVISIBLE

        val textView = view.findViewById<TextView>(R.id.text)
        textView.text = text

        val descriptionView = view.findViewById<TextView>(R.id.description)
        descriptionView.text = description
        if (descriptionColor != 0)
            descriptionView.setTextColor(descriptionColor)

        if (onClickListener != null)
            view.setOnClickListener(onClickListener)
    }


    fun setEditableAccessible(view: View, icon: Int = -1, text: String = "", hint: String = "", editText: String = "", description: String = "", editColor: Int = 0, descriptionColor: Int = 0,
                              textWatcher: OnTextChangeListener? = null, onClickListener: View.OnClickListener? = null) {
        if (icon != -1) {
            val imageView = view.findViewById<ImageView>(R.id.icon)
            imageView.visibility = View.VISIBLE
            imageView.setImageResource(icon)
        }
        val textView = view.findViewById<TextView>(R.id.text)
        textView.text = text

        val inputView = view.findViewById<EditText>(R.id.input)
        inputView.hint = hint
        inputView.setText(editText)

        if (editColor != 0) {
            inputView.setTextColor(editColor)
        }

        if (textWatcher != null)
            RxTextView.textChanges(inputView)
                    .skipInitialValue()
                    .debounce(500, TimeUnit.MICROSECONDS)
                    .subscribe {
                        textWatcher.textChange(it.toString())
                    }

//        inputView.addTextChangedListener(textWatcher)

        val descriptionView = view.findViewById<TextView>(R.id.description)
        descriptionView.text = description
        if (descriptionColor != 0) {
            descriptionView.setTextColor(descriptionColor)
        }
        if (onClickListener != null)
            descriptionView.setOnClickListener(onClickListener)
    }


}